//
//  VideoBackSeeViewController.m
//  Rail
//
//  Created by hychen on 2018/3/8.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "VideoBackSeeViewController.h"
#import "BMSelectedTextField.h"
#import "TimeScrollView.h"
#import "NSDate+Category_lib.h"
#import "HcdDateTimePickerView.h"

@interface VideoBackSeeViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UILabel *descLabel;

@property (nonatomic, strong) UITextField *startField;
@property (nonatomic, strong) HcdDateTimePickerView *dateTimePickerView;

//@property (nonatomic, strong) TimeScrollView *timeScrollView;

@property (nonatomic, strong) UIButton *backButton;

@property (nonatomic, strong) NSDate *backSeeDate;
@end

@implementation VideoBackSeeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _backSeeDate = [NSDate date];
    
    BMWeakSelf
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.bounds];
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    
    self.descLabel = [UILabel labelWithText:@"请选择要回看的时间" textColor:Color_RGB(167, 160, 157) font:[UIFont systemFontOfSize:12.f]];
    self.descLabel.textAlignment = NSTextAlignmentCenter;
    [self.scrollView addSubview:self.descLabel];
    [self.descLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.scrollView).offset(15);
        make.centerX.equalTo(weakSelf.scrollView);
        make.top.mas_equalTo(40);
    }];
    
//    BMDatePickerViewStyle *style = [BMDatePickerViewStyle new];
//    style.datePickerMode = UIDatePickerModeDate;
//    style.maximumDate = [NSDate date];
//    style.minimumDate = [NSDate dateWithDaysBeforeNow:365];
//    style.date = [NSDate date];
    
//    self.startField = [BMSelectedTextField new];
//    self.startField.font = [UIFont systemFontOfSize:14.f];
//    self.startField.textColor = Color_Hex(0x565656);
//    self.startField.placeholder = @"点击选择回放日期";
//    self.startField.textAlignment = NSTextAlignmentCenter;
//    self.startField.text = [style.date formatYearMonthDay_lib];
//    //请选择回放开始时间
//    [self.startField showDatePickerWithToolBarText:@"" style:style toolBarStyle:BMPickerViewToolBarStyleText cancelBlock:^{
//
//    } clickBlock:^(UITextField *textField, NSDate *selectedDate) {
//        //选择年月日
//        weakSelf.fromDate = selectedDate;
//    }];
//    [self.scrollView addSubview:self.startField];
//    [self.startField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(100);
//        make.height.mas_equalTo(30);
//        make.top.equalTo(self.descLabel.mas_bottom).offset(20);
//        make.centerX.equalTo(self.descLabel);
//    }];
    
    self.startField = [UITextField new];
    self.startField.font = [UIFont systemFontOfSize:18.f];
    self.startField.textColor = Color_Hex(0x565656);
    self.startField.placeholder = @"点击选择回放日期";
    self.startField.textAlignment = NSTextAlignmentCenter;
    self.startField.text = [_backSeeDate formatYearMonthDayHourSecond_lib];
    self.startField.delegate = self;
    [self.scrollView addSubview:self.startField];
    [self.startField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(240);
        make.height.mas_equalTo(60);
        make.top.equalTo(self.descLabel.mas_bottom).offset(20);
        make.centerX.equalTo(self.descLabel);
    }];
    
    UIImageView *arrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"home_arrow_ic"]];
    [self.scrollView insertSubview:arrow belowSubview:self.startField];
    [arrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(12);
        make.left.equalTo(weakSelf.startField.mas_right);
        make.centerY.equalTo(weakSelf.startField);
    }];
    
//    self.endField = [BMSelectedTextField new];
//    self.endField.font = [UIFont systemFontOfSize:13.f];
//    self.endField.textColor = Color_Hex(0x565656);
//    [self.endField showDatePickerWithToolBarText:@"" style:style toolBarStyle:BMPickerViewToolBarStyleText cancelBlock:^{
//
//    } clickBlock:^(UITextField *textField, NSDate *selectedDate) {
//
//    }];
//    [self.view addSubview:self.endField];
//    [self.endField mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.mas_equalTo(100);
//        make.height.mas_equalTo(30);
//        make.top.equalTo(self.startField.mas_bottom).offset(20);
//        make.centerX.equalTo(self.descLabel);
//    }];
    
//    self.timeScrollView = [[TimeScrollView alloc] init];
//    self.timeScrollView.frame = CGRectMake(10, 115, SCREEN_WIDTH-20, 60);
//    self.timeScrollView.backgroundColor = [UIColor clearColor];
//    [self.scrollView addSubview:self.timeScrollView];
    
    self.backButton = [UIButton buttonWithTitle:@"回看" titleColor:[UIColor whiteColor] norImage:ImageNamed_bb(@"app_btn_n") highImage:ImageNamed_bb(@"app_btn_p") action:@selector(clickBackButton) owner:self];
    self.backButton.layer.cornerRadius = 22.5f;
    [self.scrollView addSubview:self.backButton];
    [self.backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.scrollView);
        make.left.mas_equalTo(25);
        make.top.mas_equalTo(weakSelf.startField.mas_bottom).offset(40);
        make.height.mas_equalTo(45);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)clickBackButton {
    if (!self.backSeeDate) {
        Show_Msg_Error(@"请选择回放开始时间")
        return;
    }
    
    if (self.BlockClickSeeBack) {
        self.BlockClickSeeBack(self.backSeeDate);
    }
}


#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    [[UIApplication sharedApplication].keyWindow addSubview:self.dateTimePickerView];
    self.dateTimePickerView.maxDate = [NSDate date];
    [self.dateTimePickerView showHcdDateTimePicker];
    return NO;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - getter
- (HcdDateTimePickerView *)dateTimePickerView {
    if (!_dateTimePickerView) {
        _dateTimePickerView = [[HcdDateTimePickerView alloc] initWithDatePickerMode:DatePickerDateTimeMode defaultDateTime:_backSeeDate];
        _dateTimePickerView.topViewColor = Color_RGB(247, 247, 247);
        _dateTimePickerView.buttonTitleColor = Color_RGB(75, 200, 175);
        BMWeakSelf
        [_dateTimePickerView setClickedOkBtn:^(NSString *dateTimeStr) {
            weakSelf.startField.text = dateTimeStr;
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
            //赋值日期
            weakSelf.backSeeDate = [fmt dateFromString:dateTimeStr];
        }];
        NSInteger currYear = _backSeeDate.year;
        [_dateTimePickerView setMinYear:currYear-1];
        [_dateTimePickerView setMaxYear:currYear];
    }
    return _dateTimePickerView;
}

@end
